package com.avic.rong.wedgit.customView;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.pickerview.TimePickerView;
import com.avic.rong.R;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.DensityUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Creater: Created by wangyz on 2019/9/5.
 * Email: 936804097@qq.com
 */
public class SearchItemView extends LinearLayout implements View.OnClickListener,SearchItermAdapter.MoreInterface,SearchItermAdapter.ItemSelectedInterface {


    private RecyclerView recycler_list;
    private ImageView img_arrow;
    public TextView tv_show_select;//显示的文本
    private TextView tv_top_title;//标题
    public TextView tv_start;
    public TextView tv_end;
    private RelativeLayout rl_time_group;//时间布局
    private SearchItermAdapter searchItermAdapter;
    private SearchWordItermAdapter wordItermAdapter;
    public ItemSearchBean itemSearchBean;
    //    private RecyclerView recycler_list_attrs;//文本基本属性显示
    private boolean isFold = true;
    private Context context;
    private GridLayoutManager gridLayoutManager;

    //二级列表
    private SearchItermAdapter searchItermAdapterSecond;
    private GridLayoutManager gridLayoutManagerSecond;
    private RecyclerView recycler_list_second;
    private LinearLayout ll_group_second;//箭头组合
    private LinearLayout ll_arrow_second;
    private ImageView img_arrow_second;
    private boolean isFoldSecond = true;


    public SearchItemView(Context context) {
        this(context, null);
    }

    public SearchItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        LayoutInflater mInflater = LayoutInflater.from(context);
        View myView = mInflater.inflate(R.layout.view_search_item, null);
        addView(myView);
        recycler_list = myView.findViewById(R.id.recycler_list);
        img_arrow = myView.findViewById(R.id.img_arrow);
//        recycler_list_attrs =  myView.findViewById(R.id.recycler_list_attrs);//文本属性
        tv_show_select = myView.findViewById(R.id.tv_show_select);
        tv_top_title = myView.findViewById(R.id.tv_top_title);
        tv_start = myView.findViewById(R.id.tv_start);
        tv_end = myView.findViewById(R.id.tv_end);
        rl_time_group = myView.findViewById(R.id.rl_time_group);
        searchItermAdapter = new SearchItermAdapter(context, this);
        searchItermAdapter.setMoreInterface(this);
        gridLayoutManager = new GridLayoutManager(context, 3);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION,8);//top间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, DensityUtils.dp2px(context, 6));//底部间距
//        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION,50);//左间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION,DensityUtils.dp2px(context,6));//右间距
        recycler_list.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));


        recycler_list.setLayoutManager(gridLayoutManager);
        recycler_list.setAdapter(searchItermAdapter);

        //文本
        tv_show_select.setOnClickListener(this);
        img_arrow.setOnClickListener(this);
        tv_start.setOnClickListener(this);
        tv_end.setOnClickListener(this);

        //二级
        ll_group_second = myView.findViewById(R.id.ll_group_second);
        ll_arrow_second = myView.findViewById(R.id.ll_arrow_second);
        img_arrow_second = myView.findViewById(R.id.img_arrow_second);
        recycler_list_second = myView.findViewById(R.id.recycler_list_second);
        ll_group_second = myView.findViewById(R.id.ll_group_second);
        ll_arrow_second.setOnClickListener(this);

        searchItermAdapterSecond = new SearchItermAdapter(context,this);
        gridLayoutManagerSecond = new GridLayoutManager(context, 3);
        searchItermAdapter.setItemSelectedInterface(this);
        recycler_list_second.setLayoutManager(gridLayoutManagerSecond);
        recycler_list_second.setAdapter(searchItermAdapterSecond);
        recycler_list_second.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));

        //2019-09-19
        ll_arrow_second.setVisibility(View.GONE);

    }


    public void setSearchBeanList(ItemSearchBean itemSearchBean,boolean clearData) {
        this.itemSearchBean = itemSearchBean;

        //-------------调整控制子adapter显示start
        searchItermAdapter.addItemNums(itemSearchBean.getMaxCountFold());
        gridLayoutManager.setSpanCount(itemSearchBean.getSpanCount());
        searchItermAdapter.setEndMoreShow(itemSearchBean.isEndMoreShow());
        searchItermAdapter.setMultiSelected(itemSearchBean.isMultiSelected());

        searchItermAdapter.openCloseStatus(itemSearchBean.isFold());//是否折叠全部

        if (itemSearchBean.getMoreInterface() != null){//更多按钮事件
            searchItermAdapter.setMoreInterface(itemSearchBean.getMoreInterface());
        }

        //-------------调整控制子adapter显示end


        tv_top_title.setText(itemSearchBean.getName());
        if (clearData){//如果是清空数据
            tv_start.setText(ConstantsUtils.EMPTY_STR);
            tv_end.setText(ConstantsUtils.EMPTY_STR);
            itemSearchBean.setStartTime(ConstantsUtils.EMPTY_STR);
            itemSearchBean.setEndTime(ConstantsUtils.EMPTY_STR);
        }

        if (itemSearchBean.isShowTimeGroupLayout()){
            rl_time_group.setVisibility(View.VISIBLE);
        }else{
            rl_time_group.setVisibility(View.GONE);
        }

        if (itemSearchBean.isCollapseEnable()){//如果支持塌缩
            searchItermAdapter.openCloseStatus(false);
        }
        searchItermAdapter.setDataList(itemSearchBean.getSubBeanList());
        searchItermAdapter.notifyDataSetChanged();


        if (itemSearchBean.isCollapseEnable()){//如果支持塌缩
            //二级清空
            searchItermAdapterSecond.notifyDataSetChanged();
            ll_group_second.setVisibility(View.GONE);
        }
    }


    public void changeBirthday(final TextView view) {
        TimePickerView pvTime = new TimePickerView(context, TimePickerView.Type.YEAR_MONTH_DAY);
        Calendar calendar = Calendar.getInstance();
        pvTime.setRange(calendar.get(Calendar.YEAR) - 50, calendar.get(Calendar.YEAR));//要在setTime 之前才有效果哦
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        pvTime.setTitle("选择时间");
//        pvTime.setTime(new Date(System.currentTimeMillis()));
        //时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                int viewId = view.getId();
                long startTime = 0L;
                long endTime = 0L;
                if (viewId == R.id.tv_start) {
                    startTime = date.getTime();
                    if (!TextUtils.isEmpty(itemSearchBean.getEndTime())) {
                        endTime = DateUtils.dateToStamp(itemSearchBean.getEndTime(), DateUtils.FORMAT_SHORT_CN);
                    }

                } else {
                    if (!TextUtils.isEmpty(itemSearchBean.getStartTime())) {
                        startTime = DateUtils.dateToStamp(itemSearchBean.getStartTime(), DateUtils.FORMAT_SHORT_CN);
                    }
                    endTime = date.getTime();
                }

                if (startTime > 0 && endTime > 0) {
                    if (startTime > endTime) {
                        String tipStr = "请选择合适开始时间！";
                        if (viewId == R.id.tv_end) {
                            tipStr = "请选择合适结束时间！";
                        }
                        Toast toast = Toast.makeText(context, tipStr, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return;
                    }
                }


                view.setText(DateUtils.getFormatShort(date.getTime(),DateUtils.FORMAT_SHORT_CN));
                if (viewId == R.id.tv_start) {
                    itemSearchBean.setStartTime(view.getText().toString());
                } else {
                    itemSearchBean.setEndTime(view.getText().toString());
                }
                searchItermAdapter.clearTabStatus();
            }
        });
        pvTime.show();
    }


    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_show_select || viewId == R.id.img_arrow) {
            searchItermAdapter.openCloseStatus(!isFold);
            if (isFold) {
                ObjectAnimator.ofFloat(img_arrow, "rotation", 0, 180).start();
            } else {
                ObjectAnimator.ofFloat(img_arrow, "rotation", -180, 0).start();
            }
            isFold = !isFold;
        } else if (viewId == R.id.tv_start) {
            changeBirthday(tv_start);
        } else if (viewId == R.id.tv_end) {
            changeBirthday(tv_end);
        }else if(viewId == R.id.ll_arrow_second){//二级选择
            if (isFoldSecond) {
                ObjectAnimator.ofFloat(img_arrow_second, "rotation", 0, 180).start();
            } else {
                ObjectAnimator.ofFloat(img_arrow_second, "rotation", -180, 0).start();
            }
            isFoldSecond = !isFoldSecond;
            searchItermAdapterSecond.openCloseStatus(isFoldSecond);
//            searchItermAdapterSecond.notifyDataSetChanged();
        }


    }


    public void setEndMoreShow(boolean show){
        searchItermAdapter.setEndMoreShow(show);
    }


    public void setSpanCount(int count){
        gridLayoutManager.setSpanCount(count);
    }

    public void setMaxCountFold(int count){
        searchItermAdapter.addItemNums(count);

    }

    public void  setMultiSelected(boolean multiSelected){
        searchItermAdapter.setMultiSelected(multiSelected);
    }


    public void notifyDataChangeShowSelect(){
        if (searchItermAdapter != null){
            searchItermAdapter.notifyDataChangeShowSelect();
        }
    }

    public void clearTimeWidget() {
        this.tv_start.setText(ConstantsUtils.EMPTY_STR);
        this.tv_end.setText(ConstantsUtils.EMPTY_STR);
        itemSearchBean.setEndTime(ConstantsUtils.EMPTY_STR);
        itemSearchBean.setStartTime(ConstantsUtils.EMPTY_STR);
    }

    @Override
    public void moreClick() {
        if (viewMoreInterface != null){
            viewMoreInterface.moreClick();
        }
    }

    private ViewMoreInterface viewMoreInterface;

    public void setViewMoreInterface(ViewMoreInterface viewMoreInterface) {
        this.viewMoreInterface = viewMoreInterface;
    }


    public interface ViewMoreInterface{
        public void moreClick();
    }


    @Override
    public void selectItem(ItemSearchBean.SubBean selectedSubBean) {//第一级选中
        List<ItemSearchBean.SubBean> childList = new ArrayList<>();
        for(ItemSearchBean.SubBean subBean : itemSearchBean.getSubBeanList()){
            if (subBean.getId() == selectedSubBean.getId()){
                childList = subBean.getChildList();
                break;
            }
        }

        if (childList.size() > 0){
            ll_group_second.setVisibility(View.VISIBLE);
            ll_arrow_second.setVisibility(View.VISIBLE);
        }else{
            ll_group_second.setVisibility(View.GONE);
            ll_arrow_second.setVisibility(View.GONE);
        }
        searchItermAdapterSecond.setDataList(childList);
        searchItermAdapterSecond.addItemNums(1);
        searchItermAdapterSecond.openCloseStatus(false);

    }


}
